package com.douqu.game.core.entity.world.map;

import com.douqu.game.core.e.E_ActionPlayerType;
import com.douqu.game.core.e.E_BattleDetailType;
import com.douqu.game.core.entity.EntityObject;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.CoreUtils;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2018-04-26 15:36
 */
public class MapMatchTeam extends EntityObject{

    private String id;

    private SpriteBean player;

    private SpriteBean target;

    /**
     * 这里面包含地图数据
     */
    private int mapId;

    private int cityId;

    private long createTime;

    private int waitTime;

    public MapMatchTeam(SpriteBean player, SpriteBean target, int waitTime, int cityId, int mapId)
    {
        if(!(player instanceof SpritePlayerBean) && target instanceof SpritePlayerBean)
        {
            this.player = target;
            this.target = player;
        }
        else
        {
            this.player = player;
            this.target = target;
        }

        this.player.setStatus(SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_PREPARE);
        this.target.setStatus(SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_PREPARE);

        this.createTime = DataFactory.currentTime;
        this.waitTime = waitTime;
        this.cityId = cityId;
        this.mapId = mapId;
        this.id = CoreUtils.createBattleId(this.player.getObjectIndex(), this.target.getObjectIndex());
    }

    public boolean isOverdue()
    {
        return DataFactory.currentTime - createTime >= 60000;
    }

    public boolean isStartBattle()
    {
        return DataFactory.currentTime - createTime >= waitTime;
    }

    public void destroy()
    {
        player.setStatus(SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_MATCH);
        target.setStatus(SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_MATCH);
    }

    public void battleStatus()
    {
        player.setStatus(SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_BATTLE);
        target.setStatus(SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_BATTLE);
    }


    public E_BattleDetailType getBattleDetailType()
    {
        //此方法一定要保证Player是在城里的时候
        if(target instanceof SpritePlayerBean)
        {
            SpritePlayerBean targetPlayerBean = (SpritePlayerBean) target;
            if(targetPlayerBean.getActionPlayerType() == E_ActionPlayerType.ONLINE_MAP && targetPlayerBean.isInCity())
                return E_BattleDetailType.PVP_WORLD_MAP;
            else
                return E_BattleDetailType.PVP_DATA_WORLD_MAP;
        }
        else
            return E_BattleDetailType.PVE_WORLD_MAP;
    }


    public SpriteBean getPlayer() {
        return player;
    }

    public void setPlayer(SpriteBean player) {
        this.player = player;
    }

    public SpriteBean getTarget() {
        return target;
    }

    public void setTarget(SpriteBean target) {
        this.target = target;
    }

    public long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getWaitTime() {
        return waitTime;
    }

    public void setWaitTime(int waitTime) {
        this.waitTime = waitTime;
    }


    public int getMapId() {
        return mapId;
    }

    public void setMapId(int mapId) {
        this.mapId = mapId;
    }

    public int getCityId() {
        return cityId;
    }

    public void setCityId(int cityId) {
        this.cityId = cityId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof MapMatchTeam)) return false;

        MapMatchTeam that = (MapMatchTeam) o;

        if (id != null ? !id.equals(that.id) : that.id != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return id != null ? id.hashCode() : 0;
    }

    @Override
    public String toString() {
        return "MapMatchTeam{" +
                "id='" + id + '\'' +
                ", player=" + player +
                ", target=" + target +
                ", createTime=" + createTime +
                ", waitTime=" + waitTime +
                "} " + super.toString();
    }
}
